package com.example.guethe.fireanalysisapp;

import android.app.Application;
import android.content.Context;


import com.example.guethe.fireanalysisapp.util.LoginHelper;

import java.io.IOException;

import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.NetProvider;
import cn.droidlover.xdroidmvp.net.RequestHandler;
import cn.droidlover.xdroidmvp.net.XApi;
import okhttp3.CookieJar;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;


/**
 * 封装自己的 Application
 * Created by zhaoshengping on 2017/8/25.
 */

public class DApplication extends Application {

    public static Application app;

    public DApplication() {
        super();
        app = this;
    }

    /**
     * 初始化
     * 1 获取APP对象
     * 2 初始化网络请求配置
     */
    @Override
    public void onCreate() {
        super.onCreate();
        app = this;

        XApi.registerProvider(new NetProvider() {

            @Override
            public Interceptor[] configInterceptors() {
                /**
                 * 配置interceptor
                 * 1 添加网络请求日志配置interceptor: HttpLoggingInterceptor
                 * 2
                 */
                HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                Interceptor[] interceptors = new Interceptor[1];
                interceptors[0] = loggingInterceptor;
                return interceptors;
            }

            @Override
            public void configHttps(OkHttpClient.Builder builder) {
                builder.addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request original = chain.request();
                        Request request = original.newBuilder()
                                .header("User-Agent", "com.drivinglm.app")
                                .header("token", LoginHelper.getUser() !=null? LoginHelper.getUser().accessToken:"")
                                .method(original.method(), original.body())
                                .build();

                        return chain.proceed(request);
                    }
                });
            }

            @Override
            public CookieJar configCookie() {
                return null;
            }

            @Override
            public RequestHandler configHandler() {
                return null;
            }

            @Override
            public long configConnectTimeoutMills() {
                return 0;
            }

            @Override
            public long configReadTimeoutMills() {
                return 0;
            }

            @Override
            public boolean dispatchProgressEnable(){
                return true;
            }

            @Override
            public boolean configLogEnable() {
                return true;
            }

            @Override
            public boolean handleError(NetError error) {
                return false;
            }
        });

    }



    public static Application getApplication (){
        return app;
    }

    public static Context getAppContext () {
        return app.getApplicationContext();
    }

    private void initSetting () {

    }
}
